/**
 * 回访详情  初始化
 */
var ConsultantRecord = {
    id: "ConsultantRecordTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

/**
 * 初始化表格的列
 */
ConsultantRecord.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId;
                return "<a href='javascript:;' onclick='ConsultantRecord.consultantDetails(" + '"' + ids + '"' + ")'>" + row.customerName + "</a>"
            }},
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true},
        {title: '门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true},
        {
            title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {
            title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {title: '成交状态', field: 'dealStatus', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (value == "已成交") {
                    return "<a href='javascript:;' onclick='ConsultantRecord.opendealStatus(" + '"' + row.reservationsId + '"' + ")'>[已成交]</a>"
                } else {
                    return "未成交";
                }
            }
        },
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true},
        { title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 120,},
        {title: '回访人', field: 'revisit', align: 'center', valign: 'middle', sortable: true},

        {
            title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true,width:450,
            formatter: function (value, row, index) {
                var reg = /,/g;
                return value.replace(reg, '');
            }
        },
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

/**
 * 查询 回访详情列表
 */
ConsultantRecord.search = function () {
    ConsultantRecord.table.refresh({query: ConsultantRecord.formParams()});
};

/**
 * 重置
 */
ConsultantRecord.resetSearch = function () {
    $("#outpatientName").val(""); //  门诊
    $("#sreach").val();  //姓名   电话 微信 预约号
    $("#startRevisitTime").val(""); //开始时间
    $("#endRevisitTime").val("");  //结束时间
    $("#tcName").val("");   //TC
    $("#followStatus").val("");
    $("#dealStatus").val("");
    $("#consumptionStatus").val("");
    $("#revisit").val("");
    document.getElementById("startRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    ConsultantRecord.search();
};

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

ConsultantRecord.inquire = function () {
    // Consultant.tracking()
    ConsultantRecord.search();
}

ConsultantRecord.downLoad = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['sreach'] = $("#sreach").val().trim();  //姓名   电话   微信  预约号
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['tcName'] = $("#tcName").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/returnVisit/returnVisitExport?' + param;
}

/**
 * 点击姓名进入详情
 */
ConsultantRecord.consultantDetails = function (ids) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantTwo/consultantDetails/' + ids
    });
    this.layerIndex = index;
}

ConsultantRecord.opendealStatus = function (id) {
    $.ajax({
        url: Feng.ctxPath + "/reservations/getTotleMoneys",
        type: "post",
        data: {
            "id": id
        },
        success: function (data) {
            alert("成交金额：" + data + "元");
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ConsultantRecord.formParams = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['sreach'] = $("#sreach").val().trim();  //姓名   电话   微信  预约号
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['tcName'] = $("#tcName").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    return queryData;
}

$(function () {
    document.getElementById("startRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    //初始化下拉TC列表
    $.post(Feng.ctxPath + "/tc/getLists", function (data) {
        var txt = '<option value="">--请选择现场TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('tcName').innerHTML = txt;
        //状态单选
        var seletetcName = $('#tcNameValue').val();
        if (seletetcName != "") {
            $("#tcName option[value=" + seletetcName + "]").attr("selected", "selected");
        }
    }, "json");

    $.post(Feng.ctxPath + "/tc/getLists", function (data) {
        var txt = '<option value="">--请选择现场TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('revisit').innerHTML = txt;
        //状态单选
        var seletetcName = $('#tcNameValue').val();
        if (seletetcName != "") {
            $("#tcName option[value=" + seletetcName + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    var defaultColunms = ConsultantRecord.initColumn();
    var table = new BSTable(ConsultantRecord.id, "/returnVisit/list1", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ConsultantRecord.formParams());
    ConsultantRecord.table = table.init();
    ConsultantRecord.search();

    var menuName = $("#menuName").val();
    $.ajax({
        url: Feng.ctxPath + "/reservation/reservation_initialization",
        type: "post",
        data: {
            "menuName": menuName
        },
        success: function (data) {
            if(data != ""){
                var item = data.listx1.split("，");
                for(var i = 0 ; i < item.length ; i++){
                    $('#ConsultantRecordTable').bootstrapTable('hideColumn', item[i]);
                }
            }
        }
    });

    //采用定时器监听 时间内容是否变化
    var oldStartValue = $("#startRevisitTime").val();
    var oldEndtimeValue = $("#endRevisitTime").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startRevisitTime").val() != oldStartValue || $("#endRevisitTime").val() != oldEndtimeValue) {//如果时间发生变化，就触发查询事件
                ConsultantRecord.search();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue = $("#startRevisitTime").val();
                oldEndtimeValue = $("#endRevisitTime").val();
            }
        }, 500);
});
